<script setup lang="ts">
import {
  useCanelOreder,
  useDeleteOrder,
  useShowPrescriptionPic
} from '@/composable'
import { OrderType } from '@/enums'
import type { ConsultOrderItem } from '@/types/consult'
import CpConsultMore from './ConsultMore.vue'
const props = defineProps<{
  item: ConsultOrderItem
}>()

// body区域,点击后去订单详情
// foot区域,根据订单状态展示按钮
// 待支付:取消问诊+去支付
// 待接诊:取消问诊+继续沟通
// 咨询中:查看处方(如果开了处方) + 继续沟通
// 已完成: 更多(查看处方,如果开了,删除订单) + 问诊记录 + (未评价?写评价:查看评价)
// 已取消:删除订单 + 咨询其他医生
// const showPopover = ref(false)
// const actions = computed(() => {
//   return [
//     { text: '查看处方', disabled: !props.item.prescriptionId },
//     { text: '删除订单' }
//   ]
// })
// 使用 查看处方方法
const { showPrescriptionPic } = useShowPrescriptionPic()

// const onSelect = async (action: { text: string }, index: number) => {
//   // 点击选项
//   // 查看处方
//   if (index === 0) {
//     action && action.text && showPrescriptionPic(props.item.prescriptionId)
//   } else {
//     // 删除订单 => 传入订单id
//     await DeleteOrder(props.item.id)
//       .then(() => {
//         showSuccessToast('删除成功')
//         emit('on-delete', props.item.id)
//       })
//       .catch(() => {
//         showFailToast('删除失败')
//       })
//   }
// }

// // 取消订单 逻辑
// // 1.定义 取消订单api
// // 2.绑定点击事件,调用 api
// // 3.成功或失败 提示
// const loading = ref(false)
// const onCancelOrder = async (item: ConsultOrderItem) => {
//   loading.value = true
//   await CancelOrder(item.id)
//     .then(() => {
//       // 修改订单状态
//       item.status = OrderType.ConsultCancel
//       item.statusValue = '已取消'
//       showSuccessToast('已取消订单')
//     })
//     .catch(() => {
//       showFailToast('取消失败')
//     })
//     .finally(() => {
//       loading.value = false
//     })
// }
const { loading, onCancelOrder } = useCanelOreder()

// 删除订单
// 1.定义api
// 2.添加提示,绑定点击事件
// 3.通知父组件修改数据
// const deleteLoading = ref(false)
const emit = defineEmits<{
  (e: 'on-delete', id: string): void
}>()
// const onDelete = async (item: ConsultOrderItem) => {
//   deleteLoading.value = true
//   // 通过是否传入 '布尔值' 判断是否发送请求删除订单
//   await DeleteOrder(item.id)
//     .then(() => {
//       showSuccessToast('删除成功')
//       emit('on-delete', props.item.id)
//     })
//     .catch(() => {
//       showFailToast('删除失败')
//     })
//     .finally(() => {
//       deleteLoading.value = false
//     })
// }
const { loading: deleteLoading, deleteConsultOrder } = useDeleteOrder(() => {
  emit('on-delete', props.item.id)
})
</script>

<template>
  <div class="consult-item" v-if="item">
    <div class="head van-hairline--bottom">
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p>极速问诊（自动分配医生）</p>
      <span
        :class="{
          orange: item.status === OrderType.ConsultPay,
          green: item.status === OrderType.ConsultComplete
        }"
        >{{ item.statusValue }}</span
      >
    </div>
    <div class="body" @click="$router.push(`/user/consult/${item.id}`)">
      <div class="body-row">
        <div class="body-label">病情描述</div>
        <div class="body-value">
          {{ item.illnessDesc }}
        </div>
      </div>
      <div class="body-row">
        <div class="body-label">价格</div>
        <div class="body-value">¥ {{ item.payment.toFixed(2) }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">创建时间</div>
        <div class="body-value tip">{{ item.createTime }}</div>
      </div>
    </div>
    <!-- 待支付 -->
    <div class="foot" v-if="item.status === OrderType.ConsultPay">
      <van-button
        @click="onCancelOrder(item)"
        :loading="loading"
        class="gray"
        plain
        size="small"
        round
        >取消订单</van-button
      >
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/user/consult/${item.id}`"
        >去支付</van-button
      >
    </div>
    <!-- 待接诊  -->
    <div class="foot" v-if="item.status === OrderType.ConsultWait">
      <van-button
        @click="onCancelOrder(item)"
        :loading="loading"
        class="gray"
        plain
        size="small"
        round
        >取消订单</van-button
      >
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${item.id}`"
        >继续沟通</van-button
      >
    </div>
    <!-- 咨询中 -->
    <div class="foot" v-if="item.status === OrderType.ConsultChat">
      <van-button
        class="gray"
        plain
        size="small"
        :disabled="!item.prescriptionId"
        round
        @click="showPrescriptionPic(item.prescriptionId)"
        >查看处方</van-button
      >
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${item.id}`"
        >继续沟通</van-button
      >
    </div>
    <!-- 已完成 -->
    <div class="foot" v-if="item.status === OrderType.ConsultComplete">
      <!-- <div class="more">
        <van-popover
          v-model:show="showPopover"
          :actions="actions"
          @select="onSelect"
          placement="top-start"
          @click="onDelete(item)"
        >
          <template #reference> 更多 </template>
        </van-popover>
      </div> -->
      <cp-consult-more
        :disabled="!item.prescriptionId"
        @on-delete="deleteConsultOrder(item)"
        @on-preview="showPrescriptionPic(item.prescriptionId)"
      ></cp-consult-more>
      <van-button
        class="gray"
        plain
        size="small"
        :to="`/room?orderId=${item.id}`"
        round
        >问诊记录</van-button
      >
      <van-button
        :class="{ gray: item.evaluateId }"
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${item.id}`"
        >{{ item.evaluateId ? '已评价' : '写评价' }}</van-button
      >
    </div>
    <!-- 已取消 -->
    <div class="foot" v-if="item.status === OrderType.ConsultCancel">
      <van-button
        class="gray"
        plain
        size="small"
        :loading="deleteLoading"
        @click="deleteConsultOrder(item)"
        :disabled="false"
        round
        >删除订单</van-button
      >
      <van-button type="primary" plain size="small" round :to="'/'"
        >咨询其他医生</van-button
      >
    </div>
  </div>
</template>

<style lang="scss" scoped>
.consult-item {
  border-radius: 4px;
  box-shadow: 0px 0px 22px 0px rgba(245, 245, 245, 0.1);
  background-color: #fff;
  margin-bottom: 10px;
  .head {
    display: flex;
    align-items: center;
    height: 50px;
    padding: 0 15px;
    .img {
      width: 20px;
      height: 20px;
    }
    > p {
      flex: 1;
      font-size: 15px;
      padding-left: 10px;
    }
    > span {
      color: var(--cp-tag);
      &.orange {
        color: #f2994a;
      }
      &.green {
        color: var(--cp-primary);
      }
    }
  }
  .body {
    padding: 15px 15px 8px 15px;
    .body-row {
      display: flex;
      margin-bottom: 7px;
    }
    .body-label {
      width: 62px;
      font-size: 13px;
      color: var(--cp-tip);
    }
    .body-value {
      width: 250px;
      &.tip {
        color: var(--cp-tip);
      }
    }
  }
  .foot {
    padding: 0 15px 15px 15px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .van-button {
      margin-left: 10px;
      padding: 0 16px;
      &.gray {
        color: var(--cp-text3);
        background-color: var(--cp-bg);
      }
    }
    .more {
      color: var(--cp-tag);
      flex: 1;
      font-size: 13px;
    }
  }
}
</style>
